package com.ktgj.customer.service.impl;

import java.text.SimpleDateFormat;
import java.util.List;

import com.ktgj.common.core.annotation.Excel;
import com.ktgj.common.core.enums.EmHelper;
import com.ktgj.customer.domain.*;
import com.ktgj.customer.domain.vo.ContractListVo;
import com.ktgj.customer.mapper.TravelContractDocumentMapper;
import com.ktgj.customer.mapper.TravelSupplierMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.mapper.TravelContractMapper;
import com.ktgj.customer.service.ITravelContractService;

/**
 * 合同2Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-10-26
 */
@Service
public class TravelContractServiceImpl implements ITravelContractService 
{
    @Autowired
    private TravelContractMapper travelContractMapper;
    @Autowired
    private TravelSupplierMapper travelSupplierMapper;
    @Autowired
    private TravelContractDocumentMapper travelContractDocumentMapper;

    /**
     * 查询合同2
     * 
     * @param contractId 合同2ID
     * @return 合同2
     */
    @Override
    public TravelContractDto selectTravelContractById(Long contractId)
    {
        return travelContractMapper.selectTravelContractById(contractId);
    }

    /**
     * 查询合同2列表
     * 
     * @param travelContractDto 合同2
     * @return 合同2
     */
    @Override
    public List<TravelContractDto> selectTravelContractList(TravelContractDto travelContractDto)
    {
        return travelContractMapper.selectTravelContractList(travelContractDto);
    }

    /**
     * 新增合同2
     * 
     * @param travelContractDto 合同2
     * @return 结果
     */
    @Override
    public int insertTravelContract(TravelContractDto travelContractDto)
    {
        return travelContractMapper.insertTravelContract(travelContractDto);
    }

    /**
     * 修改合同2
     * 
     * @param travelContractDto 合同2
     * @return 结果
     */
    @Override
    public int updateTravelContract(TravelContractDto travelContractDto)
    {
        return travelContractMapper.updateTravelContract(travelContractDto);
    }

    /**
     * 批量删除合同2
     * 
     * @param contractIds 需要删除的合同2ID
     * @return 结果
     */
    @Override
    public int deleteTravelContractByIds(Long[] contractIds)
    {
        return travelContractMapper.deleteTravelContractByIds(contractIds);
    }

    /**
     * 删除合同2信息
     * 
     * @param contractId 合同2ID
     * @return 结果
     */
    @Override
    public int deleteTravelContractById(Long contractId)
    {
        return travelContractMapper.deleteTravelContractById(contractId);
    }

    /**
     * 供应商合同列表
     * */
    @Override
    public List<TravelContract> contractList(ContractListVo contractListVo) {
        if (contractListVo.getSupplierName() != null && !contractListVo.getSupplierName().equals("")){
            TravelSupplier travelSupplier = travelSupplierMapper.travelSupplierByName(contractListVo.getSupplierName());
            contractListVo.setSupplierId(travelSupplier.getSupplierId());
        }
        List<TravelContract> travelContracts = travelContractMapper.contractList(contractListVo);
        if (travelContracts != null && travelContracts.size() > 0){
            for (TravelContract travelContract : travelContracts) {
                TravelSupplier travelSupplier = travelSupplierMapper.travelSupplierById(travelContract.getSupplierId());
                travelContract.setName(travelSupplier.getSupplierName());
                if (travelContract.getContractStatus() != null && !travelContract.getContractStatus().equals("")){
                    if (travelContract.getContractStatus().equals("1")){
                        travelContract.setStart("启用");
                    }
                    else {
                        travelContract.setStart("禁用");
                    }
                }
            }
        }
        return travelContracts;
    }

    /**
     * 添加合同
     * */
    @Override
    public int addContract(TravelContract travelContract) throws Exception
    {
        TravelSupplierDto travelSupplier = travelSupplierMapper.selectTravelSupplierById(travelContract.getSupplierId());
        travelContract.setSupplierId(travelSupplier.getSupplierId());

        if (travelContract.getStartDate()!=null)
        {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            travelContract.setStartTime(format.parse(travelContract.getStartDate()));
            travelContract.setEndTime(format.parse(travelContract.getEndDate()));
        }else {
            travelContract.setStartTime(travelContract.getStartTime());
            travelContract.setEndTime(travelContract.getEndTime());
        }
        int rows = 0;
        if (travelContract.getContractId()==null)
        {
            switch (travelContract.getCompanyPartyA()) {
                case "1":
                    travelContract.setCompanyPartyA("去玩吧");
                    break;
                case "2":
                    travelContract.setCompanyPartyA("股份");
                    break;
                case "3":
                    travelContract.setCompanyPartyA("逸程");
                    break;
            }
            rows = travelContractMapper.addContract(travelContract);
            if (rows <= 0)
            {
                throw new Exception("添加合同信息失败");
            } else
            {
                if (travelContract.getLstContractDocuments() != null && travelContract.getLstContractDocuments().size() > 0)
                {
                    for (TravelContractDocument contractDocument : travelContract.getLstContractDocuments())
                    {
                        TravelContractDocument travelContractDocument = new TravelContractDocument();
                        travelContractDocument.setTravelContractId(travelContract.getContractId());
                        travelContractDocument.setTravelContractDocumentUrl(contractDocument.getTravelContractDocumentUrl());
                        travelContractDocument.setTravelContractDocumentName(contractDocument.getTravelContractDocumentName());
                        rows = travelContractDocumentMapper.insertTravelContractDocument(travelContractDocument);
                        if (rows <= 0)
                        {
                            throw new Exception("合同附件添加失败" + contractDocument.getTravelContractDocumentName());
                        }
                    }
                }
            }
        } else
        {
            TravelContractDto travelContractDto = new TravelContractDto();
            switch (travelContract.getCompanyPartyA()) {
                case "1":
                    travelContract.setCompanyPartyA("去玩吧");
                    break;
                case "2":
                    travelContract.setCompanyPartyA("股份");
                    break;
                case "3":
                    travelContract.setCompanyPartyA("逸程");
                    break;
            }
            BeanUtils.copyProperties(travelContract, travelContractDto);
            rows = travelContractMapper.updateTravelContract(travelContractDto);
            if (rows > 0)
            {
                if (travelContract.getLstContractDocuments() != null && travelContract.getLstContractDocuments().size() > 0)
                {
                    List<TravelContractDocument> lstContractDocuments = travelContractDocumentMapper.selectContractDocumentByContractId(travelContract.getContractId());
                    for (TravelContractDocument contractDocument : travelContract.getLstContractDocuments())
                    {
                        if (contractDocument.getTravelContractDocumentId() == null)
                        {
                            contractDocument.setTravelContractId(travelContract.getContractId());
                            travelContractDocumentMapper.insertTravelContractDocument(contractDocument);
                        }
                        TravelContractDocument tmpContractDoucument = lstContractDocuments.stream().filter(item -> item.getTravelContractDocumentId().equals(contractDocument.getTravelContractDocumentId())).findFirst().orElse(null);
                        if (tmpContractDoucument != null)
                        {
                            travelContractDocumentMapper.updateTravelContractDocument(contractDocument);
                        }
                    }
                    for (TravelContractDocument contractDocument : lstContractDocuments)
                    {
                        if (!travelContract.getLstContractDocuments().stream().anyMatch(item -> item.getTravelContractDocumentId().equals(contractDocument.getTravelContractDocumentId())))
                        {
                            travelContractDocumentMapper.deleteTravelContractDocumentById(contractDocument.getTravelContractDocumentId());
                        }
                    }
                }
            }
        }
        return rows;
    }

    /**
     * 供应商合同号列表
     * */
    @Override
    public List<TravelContract> contractNum(Long supplierId) {
        List<TravelContract> travelContractList = travelContractMapper.enableContractList(supplierId);
        return travelContractList;
    }
}
